#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5,M=1e6+5;
int a[N],n;
int b[M],Mx,Tg;
void solve(){
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=0;i<=1000000;i++)b[i]=-1e18;
    Mx=0,Tg=0;
    b[0]=0;
    for(int i=1;i<=n;i++){
        int tmp=max(Mx,b[a[i]]+a[i])+Tg;
        if(a[i]==a[i-1])Tg+=a[i];
        b[a[i-1]]=max(b[a[i-1]]+Tg,tmp)-Tg;
        Mx=max(Mx,b[a[i-1]]);
    }
    cout<<Mx+Tg<<'\n';
}
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tc;
    cin>>tc;
    while(tc--)solve();
    return 0;
}
